<?php
class Tenant extends MY_Controller {

	var $currentPage = "tenant";
	var $caption = "Tenant";
	
	public function index($start=0,$limit=10) {
		
		$this->load->library('pagination');
		$config['base_url'] = base_url() . 'admin/tenant/index/';
		$config['total_rows'] = count($this->mTenant->getAllEntries());
		$config['uri_segment'] = 4;
		$config['per_page'] = $limit;
		$config['cur_page'] = $this->uri->segment(4);
		$config['use_page_numbers'] = true;
		$config['num_links'] = 5;
		$config['first_link'] = '<span class="page-far-left">';
		$config['last_link'] = '<span class="page-far-right">';
		
		$this->pagination->initialize($config);
		
		$join_tables = array($this->mBusinessnature);
	    $data['tenantlist'] = $this->mTenant->getAllEntriesWithJoin($join_tables,$limit,(($start-1) * $limit));
	    $data['pagination'] = $this->pagination->create_links();
		
		$this->loadAdminPage($data);
	}

	public function create($data='') {
		$this->action = 'add';
		
		$data['businessnatures'] = $this->mBusinessnature->getAllEntries();
		
		$this->loadAdminPage($data);
	}
	
	public function add() {
		$tenant = $this->mTenant;
		
		$tenant->name = trim($this->input->post('name',TRUE));
		$tenant->address = trim($this->input->post('address',TRUE));
		$tenant->description = trim($this->input->post('description',TRUE));
		$tenant->business_nature_id = trim($this->input->post('business_nature_id',TRUE));
		
		$this->message = INSERT_ERROR;
		
		if(strlen($tenant->name) != 0 && strlen($tenant->address) != 0 && strlen($tenant->business_nature_id) != 0) {
			if(count($this->mTenant->find_by_name($tenant->name))==0) {
				$this->message = INSERT_SUCCESS;
				$tenant->insert();
			} else {
				$this->message = HAS_DUPLICATE . " name.";
				$data['tenant'] = $tenant;
				$this->create($data);
				return;
			}
		} else {
			$data['tenant'] = $tenant;
			$this->create($data);
			return;
		}
		
		$this->index();
	}
	
	public function edit($id) {
		$tenant = $this->mTenant->getEntryById($id);
		
		if(count($tenant) == 0) {
			show_error("Invalid tenant", 500, "Invalid data");
		}
	
		$data['businessnatures'] = $this->mBusinessnature->getAllEntries();
		
		$data['tenant'] = $tenant[0];
		
		$this->action = 'edit';
		$this->id = $tenant[0]->id;
		$this->loadAdminPage($data);
	}
	
	public function update() {
		$tenant = $this->mTenant;
		
		$tenant->id = $this->input->post('id',TRUE);
		$tenant->name = trim($this->input->post('name',TRUE));
		$tenant->address = trim($this->input->post('address',TRUE));
		$tenant->description = trim($this->input->post('description',TRUE));
		$tenant->business_nature_id = $this->input->post('business_nature_id',TRUE);
		$this->message = UPDATE_ERROR;
		
		if(strlen($tenant->id) != 0 && strlen($tenant->business_nature_id) != 0 && strlen($tenant->name) != 0 && strlen($tenant->address) != 0) {
			
			if(count($this->mTenant->find_by_name_of_others($tenant->id,$tenant->name))==0) {
				if($tenant->update())
					$this->message = UPDATE_SUCCESS;
			} else {
				$this->message = HAS_DUPLICATE . " name.";
			}
		}
		
		$this->edit($tenant->id);
	}
	
	public function delete($id) {
		
		$this->message = DELETE_ERROR;
		
		if($this->mTenant->delete($id))
			$this->message = DELETE_SUCCESS;
		
		$this->index();		
	}
}
?>